import axios from 'axios';
import $ from 'jquery'
import { Message, Loading } from "element-ui";
import Bus from "./bus.js";
import Router from "../router"
import { getAuthorization } from "@/utils/common.js";
//let baseUrl = "http://192.168.8.61:9090";
//let baseUrl = "http://192.168.137.12:9999";
//let baseUrl = "http://47.108.86.1:8989";
//let baseUrl = "http://127.0.0.1:9999";
//let baseUrl = "http://192.168.137.224:9999";
let baseUrl = "/api";

axios.interceptors.response.use(function(response) {
	let authorization = response.headers.token;
	let url = response.config.url;
	if(authorization && url.indexOf("/user/login") != "-1") {
		localStorage.setItem("wisdomAuthorization", authorization);  
	}
	return response;
}, function(error) {
	return Promise.reject(error);
});

//请求数据
function requestData(url, queryParams, successCallback) {
	let orExport = false;
	switch(url) {
		case '/dataList/export':
		case '/site/export':
			orExport = true;
			break;
	}
	let that = this;
	let authorization = getAuthorization();
	let header = {};
	if(authorization){
		header.token = authorization;
	}
	let method = {
		params: queryParams,
		headers: header
	}
	if(orExport) {
		method.responseType = 'blob';
	}
	axios.get(baseUrl + url, method).then(function(res) {
		successCallback(res.data);
	}).catch(function(error) {
		console.log("error", error);
	});
}

//请求数据
function requestPostData(url, queryParams, successCallback) {
	let orExport = false;
	let that = this;
	let header = {};
	let authorization = getAuthorization();
	if(authorization){
		header.token = authorization;
	}
	axios.post(baseUrl + url, queryParams, {headers: header}).then(function(res) {
		successCallback(res.data);
	}).catch(function(error) {
		console.log("error", error);
	});
}

//导出
export function uploadData(url,ss){
	let token = localStorage.getItem("wisdomAuthorization");
  axios(baseUrl+url,{
	headers: {'token':token},
	responseType: 'blob'
  }).then((response) => {
	let filename = "";
	let fileHeaders = response.headers;
	for(let i in fileHeaders){
	  if(i == "content-disposition"){
		filename = fileHeaders[i];
	  }
	}
	if (filename) {
	  filename = filename.substring(filename.indexOf("=") + 1, filename.length);
	  filename = decodeURI(filename);
	  if(filename.indexOf('"') != "-1"){
		filename = filename.substring(1,filename.length - 1);
	  }
	  let downloadElement = document.createElement('a');
	  let blob = response.data;
	  let href = window.URL.createObjectURL(blob);
	  downloadElement.href = href;
	  downloadElement.download = filename;
	  document.body.appendChild(downloadElement);
	  downloadElement.click();
	  document.body.removeChild(downloadElement);
	  window.URL.revokeObjectURL(href);
	}
  }).catch((error) => {
	console.log(error);
  });
  }
  




export {
	requestData,
	requestPostData,
}









